package com.sfh.cpc.com.widget;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.ImageLoaderEngine;
import com.sfh.cpc.com.R;
import com.sfh.cpc.com.entity.ENote;
import com.sfh.cpc.com.entity.EUser;
import com.sfh.cpc.com.server.INoteServer;
import com.sfh.cpc.com.ui.UtilTime;
import com.sfh.cpc.com.ui.look.LookBuiler;
import com.sfh.lib.AppCacheManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述: 内容
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/7
 */
public class WidgetMassContent extends LinearLayout implements View.OnClickListener {

    TextView tvContent;

    TextView tvLabel;

    TextView tvReading;

    TextView tvFabulous;

    TextView tvMessage;

    LinearLayout recycler;

    int width;

    ENote note;

    boolean isShow = true;

    public WidgetMassContent(Context context) {
        super(context);
        this.init();
    }

    public WidgetMassContent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.init();
    }

    public WidgetMassContent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init();
    }


    private void init() {

        inflate(this.getContext(), R.layout.widget_content, this);
        this.recycler = this.find(R.id.recycler);

        this.tvContent = this.find(R.id.tvContent);
        this.tvLabel = this.find(R.id.tvLabel);

        this.tvReading = this.find(R.id.tvReading);
        this.tvFabulous = this.find(R.id.tvFabulous);
        this.tvMessage = this.find(R.id.tvMessage);

        this.tvFabulous.setOnClickListener(this);

        this.setOnClickListener(this);

    }

    public void showData(ENote note) {
        this.note = note;
        if (this.width <= 0) {
            this.post(new Runnable() {
                @Override
                public void run() {
                    width = recycler.getMeasuredWidth();
                    showUI();
                }
            });
        } else {
            showUI();
        }
    }

    private void showUI() {

        if (TextUtils.equals("1", note.getNoteVip())) {
            EUser user = AppCacheManager.getCache(BaseAppContent.USER, EUser.class);
            if (user != null && (user.isVip() || user.getUserId() == note.getUserId())) {
                isShow = true;
            } else {
                isShow = false;
            }
        }
        this.tvFabulous.setText(note.getNoteFabulousSize() + "");
        this.tvMessage.setText(note.getNoteMsgSize() + "");

        this.tvContent.setText(note.getNoteContent());
        this.tvLabel.setText(String.format("〇 %s", note.getNoteLable()));
        this.tvReading.setText(String.format("%s 人气:%s", UtilTime.getFriendlyTime(note.getNoteCreateTime()), note.getNoteReadiSize()));
        this.tvFabulous.setCompoundDrawablesWithIntrinsicBounds(note.isFabulous() ? R.drawable.like_select : R.drawable.like, 0, 0, 0);

        this.setLineImage(note.getImages());

        if (TextUtils.isEmpty(note.getNoteContent()) || !isShow) {
            this.tvContent.setVisibility(GONE);
        } else {
            this.tvContent.setVisibility(VISIBLE);
        }
        if (TextUtils.isEmpty(note.getNoteLable())) {
            this.tvLabel.setVisibility(GONE);
        } else {
            this.tvLabel.setVisibility(VISIBLE);
        }
    }

    public void updateMessageSize() {
        this.note.setNoteMsgSize(this.note.getNoteMsgSize() + 1);
        this.tvMessage.setText(String.valueOf(note.getNoteMsgSize()));
    }

    private <T> T find(int r) {

        return (T) findViewById(r);
    }

    @Override
    public void onClick(View v) {
        if (v == tvFabulous) {
            if (note.isFabulous()) {
                return;
            }
            note.setFabulous(true);
            this.tvFabulous.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like_select, 0, 0, 0);
            this.tvFabulous.setText(String.valueOf(note.getNoteFabulousSize() + 1));
            INoteServer noteServer = AppCacheManager.getCache(INoteServer.class.getSimpleName(), INoteServer.class);
            if (noteServer != null) {
                noteServer.addFabulous(this.note.getNoteId());
            }
        } else {

            INoteServer noteServer = AppCacheManager.getCache(INoteServer.class.getSimpleName(), INoteServer.class);
            if (noteServer != null) {
                noteServer.openNote(getContext(), note);
            }
        }
    }


    private void setLineImage(List<String> images) {
        final int size = images.size();
        // 1. 默认图片大小
        int ivWidth = width;
        recycler.removeAllViews();

        if (size == 1) {
            ImageView iv = newImageView(images.get(0), 0, 0, 0, ivWidth, ivWidth / 3 * 2);
            recycler.addView(iv);

        } else if (size == 2 || size == 4) {
            // 图片大小= 屏幕宽度2/1 - 间距固定值
            ivWidth = ivWidth / 2 - 10;

            int line = size / 2 + (size % 2 > 0 ? 1 : 0);
            for (int i = 0; i < line; i++) {
                LinearLayout layout = new LinearLayout(this.getContext());
                for (int j = 0, index = i * 2 + j; (index = i * 2 + j) < size && j < 2; j++) {
                    ImageView iv = newImageView(images.get(index), index, index % 2 == 0 ? 0 : 10, line > 0 ? 10 : 0, ivWidth, ivWidth);
                    layout.addView(iv);
                }
                recycler.addView(layout);
            }
        } else {
            // 图片大小= 屏幕宽度3/1 - 间距固定值
            ivWidth = ivWidth / 3 - 10;
            //5,6,7,8,9
            int line = size / 3 + (size % 3 > 0 ? 1 : 0);
            for (int i = 0; i < line; i++) {
                LinearLayout layout = new LinearLayout(this.getContext());
                for (int j = 0, index = i * 3 + j; (index = i * 3 + j) < size && j < 3; j++) {

                    ImageView iv = newImageView(images.get(index), index, index % 3 == 0 ? 0 : 10, line > 0 ? 10 : 0, ivWidth, ivWidth);
                    layout.addView(iv);
                }
                recycler.addView(layout);
            }
        }
    }

    private ImageView newImageView(String image, int index, int leftMargin, int topMargin, int ivWidth, int ivHeight) {

        ImageView iv = new ImageView(getContext());
        LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(ivWidth, ivHeight);
        layout.leftMargin = leftMargin;
        layout.topMargin = topMargin;
        iv.setLayoutParams(layout);
        if (isShow) {
            new ImageLoaderEngine(getContext(), BaseAppContent.getImageUrl(image)).roundTransformCenterCrop(7).into(iv);
        } else {
            new ImageLoaderEngine(getContext(), R.drawable.mentoring_top_adpic).roundTransformCenterCrop(7).into(iv);
        }
        iv.setTag(R.id.image_tag_2, index);
        iv.setOnClickListener(onClickListener);
        return iv;
    }

    private View.OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (!isShow) {
                // 要求VIP
                INoteServer noteServer = AppCacheManager.getCache(INoteServer.class.getSimpleName(), INoteServer.class);
                if (noteServer != null) {
                    noteServer.openNote(getContext(), note);
                }
                return;
            }

            ArrayList<String> images = new ArrayList<>(9);
            for (String url : note.getImages()) {
                images.add(BaseAppContent.getImageUrl(url));
            }
            int position = (int) view.getTag(R.id.image_tag_2);
            new LookBuiler().image(images).setPosition(position).start((Activity) getContext());
        }
    };


}
